package com.hpin.fansea.base;

import android.app.Activity;
import android.app.Application;
import android.graphics.Bitmap;

import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.umeng.analytics.MobclickAgent;

import java.util.LinkedList;
import java.util.List;

//import cn.smssdk.SMSSDK;


/**
 * Created by Aaron on 2015/7/27.
 */
public class MyApplication extends Application {

//    public static final String TAG = MyApplication.class.getSimpleName();

//    public SharedPreferences sp;

    public static MyApplication instance = null;

    //微信支付 是物业支付 还是 商品支付
    public static String wxPayType = "";

    public static MyApplication getInstance() {
        return instance;
    }

    // 存放activity
    private List<Activity> activityList = new LinkedList<Activity>();
//    /**
//     * 我的定位坐标的点
//     */
//    public double myLatitude;
//    public double myLongitude;

//    public DisplayImageOptions options;

    public void onCreate() {

        super.onCreate();
        instance = this;
//		// imageload 加载图片
        ImageLoader.getInstance().init(getSimpleConfig());

//		// 在Appliction里面设置我们的异常处理器为UncaughtExceptionHandler处理器
		CrashHandler handler = CrashHandler.getInstance();
		handler.init(getApplicationContext());

        // 注意该方法要再setContentView方法之前实现
//        // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
//        SDKInitializer.initialize(getApplicationContext());

//        sp = getSharedPreferences(Constant.CONFIG, MODE_PRIVATE);

        //极光推送
//        JPushInterface.setDebugMode(false);//是否开启debug模式
//        JPushInterface.init(this);

////        sharesdk短信验证
//        SMSSDK.initSDK(this, "996a94cb20aa", "9f1222ada3b22d2d90d53d26aa244ad2");

//        options = new DisplayImageOptions.Builder()
//                .showImageOnLoading(R.mipmap.icon_default)
//                .showImageForEmptyUri(R.mipmap.icon_default)
//                .showImageOnFail(R.mipmap.icon_default)
//                .resetViewBeforeLoading(true)
//                .cacheOnDisk(true).cacheInMemory(true)
//                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
//                .bitmapConfig(Bitmap.Config.RGB_565)
//                .considerExifParams(true)
//                .displayer(new SimpleBitmapDisplayer()).build();

        //友盟 禁止默认的页面统计方式
        MobclickAgent.openActivityDurationTrack(false);

    }

    /**
     * 比较常用的配置方案
     *
     * @return
     */
    private ImageLoaderConfiguration getSimpleConfig() {

        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true).cacheOnDisc(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT).build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext())
                .threadPoolSize(2)
                        // 线程池内加载的数量
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new WeakMemoryCache())
                .memoryCacheSize(20 * 1024 * 1024)
//				.discCache(ne)
                .discCacheSize(50 * 1024 * 1024).discCacheFileCount(100)
                        // 缓存的文件数量
                .defaultDisplayImageOptions(defaultOptions)
                .imageDownloader(
                        new BaseImageDownloader(getApplicationContext(),
                                5 * 1000, 30 * 1000)) // connectTimeout
//								.writeDebugLogs()
                .build();// 开始构建

        return config;
    }

    // 添加Activity 到容器中
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }

    public void exit() {

        try {
            for (Activity activity : activityList) {
                if (activity != null)
                    activity.finish();

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            // 友盟统计
            MobclickAgent.onKillProcess(this);
            System.exit(0);
        }

    }

    public void clearActivity() {

        try {
            for (Activity activity : activityList) {
                if (activity != null)
                    activity.finish();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
